package com.masterclient.app.utils;

import android.graphics.Bitmap;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.request.target.ImageViewTarget;

/**
 * @auther hjian
 * @date 2020/6/17.
 */
public class TransformationFixedImage extends ImageViewTarget<Bitmap> {

    private ImageView view;
    private boolean isFixedHeight;

    public TransformationFixedImage(ImageView view) {
        super(view);
        this.view = view;
        isFixedHeight = true;
    }

    public TransformationFixedImage(ImageView view, boolean isFixedHeight) {
        this(view);
        this.isFixedHeight = isFixedHeight;
    }

    @Override
    protected void setResource(Bitmap resource) {
//        Log.e("======", "setResource: " + (resource == null));
        if (resource == null) return;
        view.setImageBitmap(resource);
        int width = resource.getWidth();
        int height = resource.getHeight();
        ViewGroup.LayoutParams params = view.getLayoutParams();

        if (isFixedHeight) {
            int imageViewHeight = view.getHeight();
            float sy = (float) (imageViewHeight * 0.1) / (float) (height * 0.1);
            int imageViewWidth = (int) (width * sy);
            params.width = imageViewWidth;
        }else {
            int imageViewWidth = view.getWidth();
            float sy = (float) (imageViewWidth * 0.1) / (float) (width * 0.1);
            int imageViewHeight = (int) (height * sy);
            params.height = imageViewHeight;
        }
        view.setLayoutParams(params);
    }
}
